package com.cmcc.xdr.pcapparse.udp;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cmcc.pcap.jpcap.udp.UdpConnection;

import com.cmcc.awareness.AppInfo;
import com.cmcc.awareness.ProtoDat;
import com.cmcc.xdr.entity.HttpEntity;
import com.cmcc.xdr.pcapparse.tcp.http.HttpProtoDistinguishStatistics;
import com.cmcc.xdr.util.DateUtil;

import jpcap.packet.UDPPacket;

public class UdpXdrGenerate {

private static Logger logger = LogManager.getLogger(UdpXdrGenerate.class);
	
	public  ArrayList<HttpEntity> udpEntityList = new ArrayList<HttpEntity>();
	
	public void generate(UdpConnection udpConnection) {
		
		logger.debug("this is udp xdr generate !");
		HttpEntity udpEntity = new HttpEntity();
		
		ArrayList<UDPPacket> altemp = new ArrayList<UDPPacket>();
		Integer altemp_length = 0;
		for(UDPPacket upt:udpConnection.al){
			if(upt.length >0 ){
				altemp.add(upt);
				++altemp_length;
			}
			if(altemp_length == 8)
				break;
		}
		
		ProtoDat protod = ProtoDat.getInstance();
		AppInfo appInfo = null;
		/*    以下调用业务识别模块       */
		for(UDPPacket tp:altemp){
			appInfo = protod.appDistinguish(tp);
			if(appInfo!=null){
				HttpProtoDistinguishStatistics.getInstance().incrementDistinguish(appInfo);
				logger.debug("\n[ match app rule ]"+appInfo.toString());
				
				//填写APP_type
				String appType = appInfo.getNum_type();
				udpEntity.setAppType(appType);
				
				//填写APP_SUB_type
				String appSubType = appInfo.getNum_subtype();
				udpEntity.setAppSubType(appSubType);
				
				break;
			}
		}
		if(appInfo == null){
			HttpProtoDistinguishStatistics.getInstance().incrementUnDistinguish();
			logger.debug("\n[ do not match any app rule ]");
		}
		
	
		try {
			saveUdpConnectionToEntityList(udpConnection,udpEntity);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			logger.error(e.getCause(), e);
		}
		
		//将每个httpEntity加入httpEntityList中，调用SaveHttpTransactionInfo的save方法保存到数据库中
		udpEntityList.add(udpEntity);
		
		/**
		 * Interface
		 * ProcedureStartTime
		 * ProcedureEndTime
		 * ProtocolType
		 * AppType
		 * AppSubType
		 * IPAddressType
		 * UserIPv4
		 * UserIPv6
		 * ......
		 * 
		 * 
		 */
			
	}
	
	public void saveUdpConnectionToEntityList(UdpConnection udpConnection,HttpEntity udpEntity) throws ParseException{
		
		//填写IE和PORTAL,默认值是0，必须填写
		udpEntity.setIe(0);
		udpEntity.setPortal(0);
		
		Integer length = udpConnection.al.size();
		logger.debug("tcpConnection length:"+length);
		
		UDPPacket tpdStart = udpConnection.al.get(0);
		UDPPacket tpdEnd = udpConnection.al.get(length-1);		
		
		//计算procedure_start_time
		long timestamp_procedure_start_time = tpdStart.sec*1000 + tpdStart.usec;
		Date procedureStartTime = DateUtil.formatTimestamp(timestamp_procedure_start_time);
		udpEntity.setProcedureStartTime(procedureStartTime);
				
		//计算procedure_end_time
		long timestamp_procedure_end_time = tpdEnd.sec*1000 + tpdEnd.usec;
		Date procedureEndTime = DateUtil.formatTimestamp(timestamp_procedure_end_time);
		udpEntity.setProcedureEndTime(procedureEndTime);
		
		//填写interface
		Integer interFace = 11;
		udpEntity.setInterFace(interFace);
		
		//填写ip_address_type,ipv4是0,ipv6是1
		udpEntity.setIpAddressType(0);
				
		//填写终端用户的ipv4地址
		String userIpv4 = tpdStart.src_ip.toString();
		userIpv4 = userIpv4.replace("/", "");
		udpEntity.setUserIpv4(userIpv4);
				
		//填写终端用户ipv6地址
		String userIpv6 = "1111111111111111";
		udpEntity.setUserIpv6(userIpv6);
					
		//填写终端用户port
		Integer userPort = tpdStart.src_port;
		udpEntity.setUserPort(userPort);
					
		//填写L4 Protocol,0为tcp,1为udp
		udpEntity.setL4Protocal(0);
		
		//填写服务端ipv4地址
		String appServerIpIpv4 = tpdStart.dst_ip.toString();
		appServerIpIpv4 = appServerIpIpv4.replace("/", "");
		udpEntity.setAppServerIpIpv4(appServerIpIpv4);
					
		//填写服务端ipv6地址
		String appServerIpIpv6 = "1111111111111111";
		udpEntity.setAppServerIpIpv6(appServerIpIpv6);
					
		//填写服务端port
		Integer appServerPort = tpdStart.dst_port;
		udpEntity.setAppServerPort(appServerPort);

		
//		//计算tcpconnection的统计量指标
//		TcpStatistics tcpstatistic = new TcpStatistics(tcpConnection);
//		tcpstatistic.packetstatistics(tcpConnection);
//		
//		//填写上行流量DL DATA
//		Integer ulData = tcpstatistic.UL_Data;
//		tcpEntity.setUlData(ulData);
//				
//		//填写下行流量UL DATA
//		Integer dlData = tcpstatistic.DL_Data;
//		tcpEntity.setDlData(dlData);
//				
//		//填写上行ip包个数 
//		Integer ulIpPacket = tcpstatistic.UL_IP_Packet;
//		tcpEntity.setUlIpPacket(ulIpPacket);
//				
//		//填写下行ip包个数
//		Integer dlIpPacket = tcpstatistic.DL_IP_Packet;
//		tcpEntity.setDlIpPacket(dlIpPacket);
//				
//		//填写上行tcp乱序包
//		Integer ulDisorderIpPacket = tcpstatistic.UL_Disorder_IP_Packet;
//		tcpEntity.setUlDisorderIpPacket(ulDisorderIpPacket);
//				
//		//填写下行tcp乱序包
//		Integer dlDisorderIpPacket = tcpstatistic.DL_Disorder_IP_Packet;
//		tcpEntity.setDlDisorderIpPacket(dlDisorderIpPacket);
//				
//		//填写上行tcp重传包
//		Integer ulRetransIpPacket = tcpstatistic.UL_Retrans_IP_Packet;
//		tcpEntity.setUlRetransIpPacket(ulRetransIpPacket);
//				
//		//填写下行tcp重传包
//		Integer dlRetransIpPacket = tcpstatistic.DL_Retrans_IP_Packet;
//		tcpEntity.setDlRetransIpPacket(dlRetransIpPacket);
//				
//		//填写上行ip分片数
//		Integer ulIpFragPackets = tcpstatistic.UL_IP_FRAG_PACKETS;
//		tcpEntity.setUlIpFragPackets(ulIpFragPackets);
//				
//		//填写下行ip分片数
//		Integer dlIpFragPackets = tcpstatistic.DL_IP_FRAG_PACKETS;
//		tcpEntity.setDlIpFragPackets(dlIpFragPackets);
//				
//		//填写tcp中syn的次数
//		Integer tcpSynNum = tcpstatistic.TCP_SYN_Num;
//		tcpEntity.setTcpSynNum(tcpSynNum);
//				
//		//填写tcp中syn ack次数
//		Integer tcpSynAckNum = tcpstatistic.TCP_SYN_ACK_Num;
//		tcpEntity.setTcpSynAckNum(tcpSynAckNum);
//				
//		//填写回复syn ack的ack次数
//		Integer tcpAckNum = tcpstatistic.TCP_ACK_Num;
//		tcpEntity.setTcpAckNum(tcpAckNum);
		
//		//填写protocol type，tcp是多少？
//		tcpEntity.setProtocolType("2");
		
		
	}

}
